package cn.rongdao.jrkabao.widget;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.LinearInterpolator;

/**
 * Created by jack on 2017/9/7
 */

public class DoubleWaveView extends View {

    private Paint mPaint;
    private Path mPath;

    private int width;
    private int height;

    private float offsetX;

    private ValueAnimator valueAnimator;

    public DoubleWaveView(Context context) {
        this(context, null);
    }

    public DoubleWaveView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public DoubleWaveView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mPaint = new Paint();
        mPath = new Path();
        mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        mPaint.setAntiAlias(true);
    }

    private void initAnim() {
        valueAnimator = ValueAnimator.ofFloat(0, width);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                offsetX = (float) animation.getAnimatedValue();
                postInvalidate();
            }
        });
        valueAnimator.setDuration(3500);
        valueAnimator.setRepeatCount(ValueAnimator.INFINITE);
        valueAnimator.setInterpolator(new LinearInterpolator());
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        width = getWidth();
        height = getHeight();
        initAnim();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (null != valueAnimator && !valueAnimator.isStarted()) {
            valueAnimator.start();
        }
        mPath.reset();
        mPaint.setColor(Color.parseColor("#48ffffff"));
        mPath.moveTo(-width + offsetX, height / 5 * 3);
        for (int i = 0; i < 3; i++) {
            mPath.rQuadTo(width / 4, -height / 10 * 3, width / 2, 0);
            mPath.rQuadTo(width / 4, height / 10 * 3, width / 2, 0);
        }
        mPath.lineTo(width, height);
        mPath.lineTo(0, height);
        mPath.close();
        canvas.drawPath(mPath, mPaint);

        mPath.reset();
        mPaint.setColor(Color.parseColor("#48ffffff"));
        mPath.moveTo(-width + offsetX, height / 5 * 3);
        for (int i = 0; i < 3; i++) {
            mPath.rQuadTo(width / 4, height / 10 * 3, width / 2, 0);
            mPath.rQuadTo(width / 4, -height / 10 * 3, width / 2, 0);
        }
        mPath.lineTo(width, height);
        mPath.lineTo(0, height);
        mPath.close();
        canvas.drawPath(mPath, mPaint);
    }

    public void stopAnim() {
        if (null != valueAnimator) {
            valueAnimator.cancel();
            valueAnimator = null;
        }
    }
}